package com.east.config.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.east.dto.DicValueDTO;
import com.east.manager.CustomCache;
import com.east.utils.Constants;

import java.util.List;


public class DicValueConverter implements Converter<Integer> {
    /**
     * 返回的id
     */
    private Integer id = -1;

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData,
                                     ExcelContentProperty contentProperty,
                                     GlobalConfiguration globalConfiguration) throws Exception {
        List<DicValueDTO> dicValueList = (List<DicValueDTO>) CustomCache.CACHE.getIfPresent(Constants.CACHE_DIC_VALUE);
        dicValueList.forEach(dicValue -> {
            String stringValue = cellData.getStringValue();
            if (dicValue.getTypeValue().equals(stringValue) && contentProperty.getField().getName().equals(dicValue.getTypeCode())) {
                id = dicValue.getId();
            }
        });
        return id;
    }
}
